home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 04 / 2 / DISK0427.ZIP / READWKS.PAS < prev    next >
Pascal/Delphi Source File  |  1985-05-13  |  6KB  |  215 lines

  1.  
  2.  program readwks;   {Program to print data in a LOTUS Worksheet file. From P.C.
  3.                                       Tech Journal October 1984 J.P. Holtman
  4.                                       (201) 361-3395}
  5.  
  6.     const   {1 => floating, 2 => formula, 4 => header}
  7.        debug = 0;
  8.  
  9.     var
  10.        wks_name : string[20];
  11.        infile : file of byte;
  12.  
  13.     type
  14.        hex_string = string[4];
  15.  
  16.     function hexprt(a : integer) : hex_string;   {binary -> HEX conversion}
  17.  
  18.        const
  19.           hexit : array[0..15] of char = '0123456789ABCDEF';
  20.  
  21.        var
  22.           strout : hex_string;
  23.           i : integer;
  24.  
  25.        begin
  26.           strout := '    ';
  27.           for i := 4 downto 1 do begin
  28.              strout[i] := hexit[a and $F];
  29.              a := a shr 4;
  30.              end;
  31.           hexprt := strout;
  32.           end;
  33.  
  34.     function read_byte : byte;
  35.  
  36.        var
  37.           i : byte;
  38.  
  39.        begin
  40.           read(infile,i);
  41.           read_byte := i;
  42.           end;
  43.  
  44.     function read_word : integer;
  45.  
  46.        var
  47.           hibyte,lobyte : byte;
  48.  
  49.        begin
  50.           read(infile,lobyte);
  51.           read(infile,hibyte);
  52.           read_word := hibyte shl 8 or lobyte;
  53.           end;
  54.  
  55.  
  56.     function process_record : boolean;
  57.  
  58.        var
  59.           rec_type, i, fld_value, rec_len, word1 : integer;
  60.           rec_format, junk : byte;
  61.           column, row, fromcol, fromrow, tocol, torow : integer;
  62.           isna : boolean;
  63.           byt : array[1..8] of byte;
  64.           double : real;
  65.           char_string : string[255];
  66.  
  67.        procedure get_format;
  68.  
  69.           begin
  70.              rec_format := read_byte;
  71.              column := read_word;
  72.              row := read_word;
  73.              end;
  74.  
  75.        procedure get_double;   {convert to REAL number}
  76.  
  77.           var
  78.              sign, exponent,i : integer;
  79.              byt2left, byt2right : integer;
  80.              sum, signicand : real;
  81.  
  82.           begin
  83.              if (debug and 1) <> 0 then begin
  84.                 write('bytes=');
  85.                 for i := 1 to 8 do write(' ',copy(hexprt(byt[i]),3,2));
  86.                 end;
  87.              if (byt[1] = 255) and (byt[2] = 240) then isna := true
  88.              else begin
  89.                 isna := false;
  90.                 if (byt[1] = 0) and (byt[2] = 0) then double := 0.0
  91.                 else begin
  92.                    if (byt[1] and $80) > 0 then sign := -1
  93.                    else sign := 1;
  94.                    byt[1] := byt[1] and $7F;
  95.                    byt2left := (byt[2] and $F0) shr 4;
  96.                    byt2right := byt[2] and $0F;
  97.                    exponent := byt[1] shl 4 + byt2left - 1023;
  98.                    sum := 0;
  99.                    for i := 8 downto 3 do sum := (sum + byt[i]) / 256.0;
  100.                    signicand := 1+(byt2right/16.0)+sum/16.0;
  101.                    double := sign*(signicand*exp(ln(2.0)*exponent));
  102.                    end   end;
  103.              end;
  104.  
  105.        procedure print_loc;   {print row/column with proper spacing}
  106.  
  107.           var
  108.              char1,char2 : integer;
  109.              alpha : string[2];
  110.              val_str : string[10];
  111.  
  112.           begin
  113.              char1 := column div 26;
  114.              char2 := column mod 26;
  115.              if char1 = 0 then alpha := ' '
  116.              else alpha := chr(64+char1);
  117.              alpha := alpha + chr(65+char2);
  118.              str(row+1,val_str);
  119.              write(copy(alpha+val_str+'         ',1,9));
  120.              end;
  121.  
  122.        begin
  123.           process_record := true;
  124.           rec_type := read_word;
  125.           rec_len := read_word;
  126.           if (debug and 4) <> 0 then writeln('type=',rec_type,'  len=',rec_len);
  127.           case rec_type of   {header}
  128.              0: begin
  129.                 word1 := read_word;
  130.                 if (rec_len <> 2) or (word1 <> $404) then begin
  131.                    writeln(#7'Not valid worksheet'#7);
  132.                    halt;
  133.                    end;
  134.                 end;
  135.  
  136. {range}
  137.              6: begin
  138.                 fromcol := read_word;
  139.                 fromrow := read_word;
  140.                 tocol := read_word;
  141.                 torow := read_word;
  142.                 row := torow-fromrow;
  143.                 column := tocol-fromcol;
  144.                 write('Lower Right Corner: ');
  145.                 print_loc;
  146.                 writeln;
  147.                 end;
  148.  
  149. {integer value}
  150.              13: begin
  151.                 get_format;
  152.                 print_loc;
  153.                 fld_value := read_word;
  154.                 writeln(fld_value);
  155.                 end;
  156.  
  157. {double precision}
  158.              14: begin
  159.                 get_format;
  160.                 print_loc;
  161.                 for i := 1 to 8 do byt[9-i] := read_byte;
  162.                 get_double;
  163.                 if isna then writeln('NA')
  164.                 else writeln(double);
  165.                 end;
  166.  
  167. {character string}
  168.              15: begin
  169.                 get_format;
  170.                 print_loc;
  171.                 char_string := '';
  172.                 for i := 1 to rec_len-5 do char_string := char_string + chr(
  173.                      read_byte);
  174.                 writeln(char_string);
  175.                 end;
  176.  
  177. {formula and value}
  178.              16: begin
  179.                 get_format;
  180.                 print_loc;
  181.                 for i := 1 to 8 do byt[9-i] := read_byte;
  182.                 get_double;
  183.                 if isna then writeln('NA')
  184.                 else writeln(double);
  185.                 for i := 1 to rec_len-13 do begin   {read rest of formula and
  186.                                       discard}
  187.                    junk := read_byte;
  188.                    if (debug and 2) <> 0 then write(copy(hexprt(junk),3,2),' ');
  189.                    end;
  190.                 if (debug and 2) <> 0 then writeln;
  191.                 end;
  192.  
  193. {end of worksheet}
  194.              1: begin
  195.                 writeln('End of Worksheet');
  196.                 process_record := false;
  197.                 end;
  198.  
  199.              else
  200.                 begin   {ignore the record type}
  201.                    for i := 1 to rec_len do junk := read_byte;
  202.                    end;
  203.  
  204.              end;
  205.           end;
  206.  
  207.     begin
  208.        write('Worksheet name: ');
  209.        readln(wks_name);
  210.        assign(infile,wks_name+'.wks');
  211.        reset(infile);
  212.        repeat
  213.           until process_record = false;
  214.        end.
  215.